package base.controller;

import base.bean.RoomEntity;
import base.dao.IRoomDao;
import base.service.IRoomService;
import base.utils.ChangeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by 30509 on 2018/3/18.
 */
@Controller
@RequestMapping("/test")
public class RoominfController {
    @Autowired
    IRoomService iRoomService;


/*  @RequestMapping("/roominf")
    private ModelAndView queryRooms() throws Exception{
        List<RoomEntity> roomlist = iRoomDao.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list", roomlist);
        modelAndView.setViewName("roominf");
        return modelAndView;
    }*/

    @RequestMapping("/addroom")
    private ModelAndView addRoom(){
        ModelAndView addRoom=new ModelAndView();
        addRoom.setViewName("addroom");
        return addRoom;
    }

   @RequestMapping(value = "/addroomx", method = RequestMethod.POST)
    private ModelAndView addRoomActon(HttpServletRequest request,HttpServletResponse response) throws IOException {
        RoomEntity room =new RoomEntity();
        String roomid=request.getParameter("roomid");
        String roomtype=request.getParameter("roomtype");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html,charset=utf-8");
        String floor=request.getParameter("floor");
        String price=request.getParameter("price");
        String clps=request.getParameter("clps");
        String cips=request.getParameter("cips");
        int roomidd= ChangeUtils.parseInt(roomid,-1);
        room.setRoomid(roomidd);
        room.setRoomtype(roomtype);
        room.setFloor(floor);
        room.setPrice(price);
        int clp= ChangeUtils.parseInt(clps,-1);
        room.setClps(clp);
        int cip= ChangeUtils.parseInt(cips,-1);
        room.setCips(cip);
        System.out.println(roomtype);
        ModelAndView addRoom=new ModelAndView();
        RoomEntity rooms = iRoomService.save(room);
        if(rooms!=null){
            List<RoomEntity> list = iRoomService.findAll();
            addRoom.addObject("list", list);
            addRoom.setViewName("roominf");
            return addRoom;
        }else{
            /*弹出错误信息，不会*/
            List<RoomEntity> list = iRoomService.findAll();
            addRoom.addObject("list", list);
            addRoom.setViewName("roominf");
            return addRoom;
        }

    }

/*      @RequestMapping("/addroomx")
            private ModelAndView a(){
            ModelAndView xx=new ModelAndView();
            xx.setViewName("index");
            return xx;
    }*/
}
